package cn.zhengzhou.springbootmybatis.utils;

import cn.zhengzhou.springbootmybatis.service.UserService;
import cn.zhengzhou.springbootmybatis.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
// 动态代理UserSerive里的方法
public class ProxyUtil {
    public static <T> T createProxy(T obj) {

        T proxy = (T) Proxy.newProxyInstance(ProxyUtil.class.getClassLoader(),
                obj.getClass().getInterfaces(), new InvocationHandler() {
                    @Override   //代理  方法   参数
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        long start = System.currentTimeMillis();
                        // 拿到方法  运行
                        Object result = method.invoke(obj, args);

                        long end = System.currentTimeMillis();
                        System.out.println(method.getName() +"方法耗时"+ (end - start)/1000.0 + "s");
                        return result;
                    }
                });
        return proxy;
    }
}
